package com.elminster.test.main;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.elminster.dao.IViewDailyAccountDetailDao;
import com.elminster.dao.impl.CatalogDaoImpl;
import com.elminster.dao.impl.DetailDaoImpl;
import com.elminster.entity.ViewDailyAccountDetial;
import com.elminster.entity.base.Catalog;
import com.elminster.entity.base.Detail;
import com.elminster.entity.base.User;
import com.elminster.service.IUserService;

public class Main {

    public static void main(String[] args) {
        String[] locations = {"applicationContext.xml"};
        ClassPathXmlApplicationContext ctx = 
            new ClassPathXmlApplicationContext(locations);
//        IUserService service = (IUserService) ctx.getBean("userService");
//        
//        List<User> list = service.searchAllUser();
//        for (User user : list) {
//            System.out.println(user);
//        }
//        List<User> found = service.findUser(new User("test"));
//        System.out.println(found.size());
//        
//        System.out.println(service.getUserCount());
        
        DetailDaoImpl detailDao = (DetailDaoImpl) ctx.getBean("detailDaoImpl");
        
        List<Detail> list = detailDao.findAll();
        GregorianCalendar calendar = new GregorianCalendar();
        int month = 10;
        calendar.set(2009, month - 1, 1);
        Date start = new Date(calendar.getTimeInMillis());
        calendar.add(GregorianCalendar.MONTH, 1);
        Date end = new Date(calendar.getTimeInMillis());
        
        list = detailDao.findByDatePeriod(start, end);
        
        double sum = 0;
        double grossProfit = 0;
        Map<String, Double> classified = new HashMap<String, Double>();
        
        for (Detail detail : list) {
//            System.out.println(detail);
            grossProfit += detail.getAccount();
            String name = detail.getCatalog().getName();
            Double s = classified.get(name);
            if (null == s) {
                s = new Double(detail.getAccount());
            } else {
                s += detail.getAccount();
            }
            classified.put(name, s);
            if (detail.getAccount() > 0) {
                continue;
            }
            sum += detail.getAccount();
        }
        
        System.out.println(grossProfit);
        System.err.println(sum);
        System.err.println(classified);
        
//        IViewDailyAccountDetailDao dao = (IViewDailyAccountDetailDao) ctx.getBean("viewDailyAccountDetailDaoImpl");
//        List<ViewDailyAccountDetial> list = dao.findByUserId(1);
//        System.out.println(list);
//        
//        CatalogDAO catalogDao = (CatalogDAO) ctx.getBean("catalogDAO");
        
//        Catalog catalog = new Catalog("hibernate_test");
        
//        Detail d = new Detail();
//        d.setAmount(-1000d);
//        d.setOccurTime(new Timestamp(System.currentTimeMillis()));
//        d.setOwnId(1);
//        d.setCatalog(catalog);
//        
//        detailDao.save(d);
//        List<Catalog> catalogs = catalogDao.findByName("hibernate_test");
//        for (Catalog c : catalogs) {
//            catalogDao.delete(c);
//        }
        
        ctx.destroy(); // 关闭 Spring 容器，以触发 Bean 销毁方法的执行
    }
}
